package utils

import (
	"bytes"
	"encoding/binary"
	"encoding/hex"
	"time"
)

func DecodeHex(src []byte) ([]byte, error) {
	dst := make([]byte, 64)
	n, err := hex.Decode(dst, src)
	return dst[:n], err
}

func GetTimeHex() []byte {
	now := time.Now()
	year, month, day := now.Date()
	hour, minute, second := now.Clock()

	hexYear := uint16(year)
	hexMonth := uint8(month)
	hexDay := uint8(day)
	hexHour := uint8(hour)
	hexMinute := uint8(minute)
	hexSecond := uint8(second)

	var buffer bytes.Buffer
	binary.Write(&buffer, binary.LittleEndian, hexYear)
	binary.Write(&buffer, binary.LittleEndian, hexMonth)
	binary.Write(&buffer, binary.LittleEndian, hexDay)
	binary.Write(&buffer, binary.LittleEndian, hexHour)
	binary.Write(&buffer, binary.LittleEndian, hexMinute)
	binary.Write(&buffer, binary.LittleEndian, hexSecond)

	hexBytes := buffer.Bytes()
	return hexBytes
}

func GetTimestampHex() []byte {
	now := int32(time.Now().Unix())

	var buffer bytes.Buffer
	binary.Write(&buffer, binary.LittleEndian, now)
	return buffer.Bytes()
}
